//
//  SEGHouseDetailsViewModel.m
//  Commom
//
//  Created by 梁学彰 on 2020/6/2.
//

#import "SEGHouseDetailsViewModel.h"

@interface SEGHouseDetailsViewModel ()


@end

@implementation SEGHouseDetailsViewModel
#pragma mark - life cycle
- (instancetype)initWithRentalId:(NSString *)rentalId type:(NSString *)type {
    if (self = [super init]) {
        self.type = type;
        self.rentalId = rentalId;
        [self sendHoseDetailsRequest];
        [self sendCommonHouseType]; //房屋设施
    }
    return self;
}

#pragma mark - send request
- (void)sendHoseDetailsRequest {
    @weakify(self);
    self.houseDetailSignal = [RACSignal createSignal:^RACDisposable * _Nullable(id<RACSubscriber>  _Nonnull subscriber) {
        @strongify(self);
        if ([self.type isEqualToString:@"4"]) {
            [self sendHouseDetailsSaleRequestWithSubscriber:subscriber];
        }else {
            [self sendHouseDetailsRentalRequestWithSubscriber:subscriber];
        }
        return nil;
    }];
}

//出售详情接口
- (void)sendHouseDetailsSaleRequestWithSubscriber:(id<RACSubscriber>) subscriber {
    [[SEGDetailsellManager sharedManager] requestDetailsellWithRentalid:_rentalId finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (isSuccessed) {
            SEGDetailsellModel *model = [SEGDetailsellModel yy_modelWithDictionary:result];
            //判断是否本人发 出现咨询按钮
            self.dataModel = model.data;
            [subscriber sendNext:self.dataModel];
        }else {
            [SEGAlertView showTmepMessageWithMessage:errorMessage];
            [subscriber sendError:[NSError errorWithDomain:errorMessage code:codeType.intValue userInfo:nil]];
        }
        [subscriber sendCompleted];
    }];
}
//出租详情接口
- (void)sendHouseDetailsRentalRequestWithSubscriber:(id<RACSubscriber>) subscriber {
    [[SEGDetailsellManager sharedManager] requestDetailLeaseWithRentalid:_rentalId finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (isSuccessed) {
            SEGDetailsellModel *model = [SEGDetailsellModel yy_modelWithDictionary:result];
            //判断是否本人发 出现咨询按钮
            self.dataModel = model.data;
            [subscriber sendNext:self.dataModel];
        }else {
            [SEGAlertView showTmepMessageWithMessage:errorMessage];
            [subscriber sendError:[NSError errorWithDomain:errorMessage code:codeType.intValue userInfo:nil]];
        }
        [subscriber sendCompleted];
    }];
}

- (void)sendCommonHouseType {
    WeakSelf;
    [[SEGQuerydictlistManager sharedManager] requestHouseQuerydictlistWithType:@"houseFacility" finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (isSuccessed) {
            NSArray *arr = result[@"data"][@"dictList"];
            if (arr&&[arr isKindOfClass:[NSArray class]]) {
                NSMutableArray *houseTypeArr = [NSMutableArray array];
                for (NSDictionary *typeDic in arr) {
                    SEGQuerydictlistDictlistModel *model = [SEGQuerydictlistDictlistModel yy_modelWithDictionary:typeDic];
                    if ([model.type isEqualToString:@"houseFacility"]) [houseTypeArr addObject:model];
                }
                weakSelf.houseFacilityArray = [houseTypeArr copy];
            }
        }else {
            [SEGAlertView showTmepMessageWithMessage:errorMessage];
        }
    }];
}


- (void)sendSimilarRentalListRequestWithCityId:(NSString *)cityId {
    WeakSelf;
    [self.requestManager requestSimilarRentalListWithRentalType:_type cityId:cityId finished:^(id result, NSError *error) {
        if (!error) {
            if (result[@"data"]&&[result[@"data"] isKindOfClass:[NSArray class]]) {
                NSArray <SEGNewRentallistitemModel *>*array = result[@"data"];
                if (array.count > 0) {
                    weakSelf.houseList = array;
                }
            }
        }
    }];
}

#pragma mark - custom delegate

#pragma mark - system delegate

#pragma mark - event response
- (NSDictionary *)getShareParam {
    NSString* url = [NSString stringWithFormat:@"%@/h5/rent-sale-sharepage/?#/rentSale?rentalId=%@&rentalType=%@&app_platform_code=%@",kClientRootURL,self.dataModel.rentalId,self.dataModel.rentalType,kAppPlatformCode];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"shareImageUrlString"] = [NSString stringWithFormat:@"%@%@",kClientImageRootURL,self.houseImageArray.firstObject];
    param[@"sharetitle"] = self.dataModel.rentalTitle;
    if (self.dataModel.rentalType.integerValue == 4) {
        param[@"sharecontent"] = [NSString stringWithFormat:@"%@室%@厅%@卫%@m²%@万",self.dataModel.room,self.dataModel.hall,self.dataModel.toilet,[SEGRentalPriceManager getPriceStringWithformatValue:self.dataModel.area],[SEGRentalPriceManager getPriceStringWithformatValue:self.dataModel.price]];
    }else {
        param[@"sharecontent"] = [NSString stringWithFormat:@"%@室%@厅%@卫%@m²%@元/月",self.dataModel.room,self.dataModel.hall,self.dataModel.toilet,[SEGRentalPriceManager getPriceStringWithformatValue:self.dataModel.area],[SEGRentalPriceManager getPriceStringWithformatValue:self.dataModel.price]];
    }
    
    param[@"inviteurl"] = url ?: @"";
    
    return param;
}
#pragma mark - private methods

#pragma mark - setter and getter
- (void)setDataModel:(SEGDetailsellDataModel *)dataModel {
    _dataModel = dataModel;
    self.houseImageArray = [dataModel.housePhoto componentsSeparatedByString:@","];

//    查询 相似房源
    [self sendSimilarRentalListRequestWithCityId:_dataModel.cityId];
}

- (SEGHouseRentalAndSaleRequestManager *)requestManager {
    if (!_requestManager) {
        _requestManager = [[SEGHouseRentalAndSaleRequestManager alloc] init];
    }
    return _requestManager;
}
@end
